package org.exia.minisig.swing.view.common;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.apache.commons.lang3.*;
import org.exia.minisig.model.object.Itinerary;
import org.exia.minisig.model.object.News;
import org.exia.minisig.model.object.Place;
import org.exia.minisig.swing.controller.HomeFramMode;
import org.exia.minisig.swing.view.home.HomeFram;

@SuppressWarnings("serial")
public class AddPanel extends JPanel {
 //VAR
 private HomeFram parentFram;
 private String data[];
 private JTextField fieldURL; 
 private String data2[];
 private JList<String> listeURL;
 private JList<String> listeURLurl;
 private JTextField labelField;
 private JTextArea textArea;
 //CONSTRUCTOR
 public AddPanel(String s, HomeFram _parentFram){
  initializeVar(s, _parentFram);
 }
 
 public void initializeVar(String s, HomeFram _parentFram){
	 this.setBackground(Color.white);
  this.textArea = new JTextArea();
  this.listeURLurl = new JList<>(data2);
  this.listeURL = new JList<>(data);
  this.data  = new String[]{};
  this.fieldURL = new JTextField();
  this.data2  = new String[]{};
  this.parentFram = _parentFram;
  this.labelField = new JTextField();
  ////
  if (this.parentFram.getMode() == HomeFramMode.PLACE){
	  fillPanelPlace();
  } else {
	  fillPanel();
  }
 }
 
 public void fillPanel(){
  this.removeAll();
  this.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
  this.setLayout(new GridBagLayout());
  GridBagConstraints gc = new GridBagConstraints();
  gc.fill=GridBagConstraints.BOTH;
  //PARAMETER
  gc.weightx=1;
  gc.weighty=1;
  //---------------------------------------//
  //TITRE
  gc.gridheight=1;
  gc.gridwidth= 4;
  gc.gridx=0;
  gc.gridy=0;
  gc.insets = new Insets(0, 0, 5, 0);
  JPanel panelTitre = new JPanel(new BorderLayout());
  JLabel titre = new JLabel("Ajout " );
  
  titre.setBackground(Color.white);
	titre.setOpaque(true);
  
  titre.setVerticalAlignment(SwingConstants.CENTER);
  titre.setHorizontalAlignment(SwingConstants.CENTER);
  panelTitre.add(titre, BorderLayout.CENTER);
  this.add(panelTitre, gc);
  //--------------------------------RDC------------------------------//
  //ETAGE 01
  //label 
  gc.insets = new Insets(0, 10, 0, 0);
  gc.gridwidth = 1;
  gc.gridheight = 1;
  gc.gridx=0;
  gc.gridy=1;
  JLabel labelLabel = new JLabel("Label :");
  labelLabel.setHorizontalAlignment(SwingConstants.CENTER);
  this.add(labelLabel, gc);
  //field
  gc.insets = new Insets(5, 10, 5, 10);
  gc.gridwidth = 3;
  gc.gridheight = 1;
  gc.gridx=1;
  gc.gridy=1;
  JPanel panelFieldLabel = new JPanel(new BorderLayout(0,0));
  
  labelField.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
  labelField.setMargin(new Insets(50, 0, 0, 0));
  labelField.setBorder(BorderFactory.createCompoundBorder(
    labelField.getBorder(), 
        BorderFactory.createEmptyBorder(0, 10, 0, 10)));
  panelFieldLabel.add(labelField, BorderLayout.CENTER);
  this.add(panelFieldLabel, gc);
  
  //ETAGE TEXT LONG
  gc.insets = new Insets(3, 5, 3, 5);
  gc.gridwidth = 4;
  gc.gridheight = 1;
  gc.gridx=0;
  gc.gridy=2;
  //TEST
  JPanel panel = new JPanel();
  panel.setLayout(new BorderLayout()); 
  JScrollPane scroll = new JScrollPane(textArea);
  panel.add(scroll, BorderLayout.CENTER);
  this.add(panel, gc);
  
  
  //ETAGE IMAGE 
  //BTN +
  gc.gridwidth = 2;
  gc.gridheight = 1;
  gc.gridx=0;
  gc.gridy=3;
  JButton btnPlus = new JButton("Ajout img");
  btnPlus.addActionListener(new ActionListener() {
   
  @Override
  public void actionPerformed(ActionEvent e) {    
   JFileChooser chooser = new JFileChooser();
      FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & GIF Images", "jpg", "gif","png");
      chooser.setFileFilter(filter);
      int returnVal = chooser.showOpenDialog(null);
      if(returnVal == JFileChooser.APPROVE_OPTION) {
       String y = chooser.getSelectedFile().getAbsolutePath();
       addImg(y);
      }     
  }
  });
  
  btnPlus.setFocusPainted(false);
  this.add(btnPlus, gc);
  //BTN -
  gc.gridwidth = 2;
  gc.gridheight = 1;
  gc.gridx=2;
  gc.gridy=3;
  JButton btnLess = new JButton("Supprime img");
  btnLess.addActionListener(new ActionListener() {
   @Override
   public void actionPerformed(ActionEvent arg0) {
    if (listeURL.getSelectedValue() != null) {
        int z= listeURL.getSelectedIndex();
        
     data = ArrayUtils.remove(data,z );
     fillPanel();
    }
   }
  });
  
  this.add(btnLess, gc);
  //ETAGE LISTE  URL IMAGE
  gc.insets = new Insets(3, 5, 3, 5);
  gc.gridwidth = 4;
  gc.gridheight = 3;
  gc.gridx=0;
  gc.gridy=4;
  
  this.listeURL = new JList<>(data);
  this.listeURL.setVisibleRowCount(5);
  JScrollPane panelListImg = new JScrollPane(this.listeURL);
  this.add(panelListImg, gc);
  //----------------------------------------------------------TRAVAIL SUR URL---------------------------------------------------------------------//
  //URL ETAGE BTN
  gc.gridwidth = 2;
  gc.gridheight = 1;
  gc.gridx=0;
  gc.gridy=8;
  
  this.fieldURL.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
  this.fieldURL.setMargin(new Insets(50, 0, 0, 0));
  this.fieldURL.setBorder(BorderFactory.createCompoundBorder(
  this.fieldURL.getBorder(), 
  BorderFactory.createEmptyBorder(0, 10, 0, 10)));
  this.add(this.fieldURL, gc);
  //ETAGE BTN
  //+
  gc.gridwidth = 1;
  gc.gridheight = 1;
  gc.gridx=2;
  gc.gridy=8;
  JButton btnPlusURL = new JButton("Ajout");
  btnPlusURL.addActionListener(new ActionListener() {
   
  @Override
  public void actionPerformed(ActionEvent arg0) {
   String x =fieldURL.getText();
    addUrl(x);
   }
  });
  
  btnPlus.setFocusPainted(false);
  this.add(btnPlusURL, gc);
  //-
  gc.gridwidth = 1;
  gc.gridheight = 1;
  gc.gridx=3;
  gc.gridy=8;
  JButton btnLessURL = new JButton("Del");
  btnLessURL.addActionListener(new ActionListener() {
   @Override
   public void actionPerformed(ActionEvent arg0) {
    if (listeURLurl.getSelectedValue() != null) {
        int z= listeURLurl.getSelectedIndex();
     data2 = ArrayUtils.remove(data2,z );
     fillPanel();
    }
    
   }
  });
  btnLessURL.setFocusPainted(false);
  this.add(btnLessURL, gc);
  
  //ETAGE LISTE  URL URL
  gc.insets = new Insets(3, 5, 3, 5);
  gc.gridwidth = 4;
  gc.gridheight = 3;
  gc.gridx=0;
  gc.gridy=9;
  
  this.listeURLurl = new JList<>(data2);
  this.listeURLurl.setVisibleRowCount(5);
  JScrollPane panelListUrl = new JScrollPane(this.listeURLurl);
  this.add(panelListUrl, gc);
  
  //ETAGE BTN
  gc.gridwidth = 2;
  gc.gridheight = 1;
  gc.gridx=0;
  gc.gridy=12;
  JButton btnSave = new JButton("Save");
  btnSave.setFocusPainted(false);
  this.add(btnSave, gc);
  btnSave.addActionListener(new ActionListener() {
   @Override
   public void actionPerformed(ActionEvent arg0) {
    //ICI LE AJOUT BDD//
    switch (parentFram.getMode()) {
    case ITINERARY:
     Itinerary newItinerary = new Itinerary();
     newItinerary.setLabel(labelField.getText());
     newItinerary.setText(textArea.getText());
     for (String img: data) {
      newItinerary.addImage(img); 
     }
     for (String url: data2) {
      newItinerary.addURL(url); 
     }
     parentFram.getServiceItinerary().addItinerary(newItinerary);
     break;
    case PLACE:
    
     break;
    case NEWS:
    	News newNews = new News();
    	newNews.setLabel(labelField.getText());
    	newNews.setText(textArea.getText());
        for (String img: data) {
        	newNews.addImage(img); 
        }
        for (String url: data2) {
        	newNews.addURL(url); 
        }
        
        parentFram.getServiceNews().addNews(newNews);
    default: 
     break;
    }
    ////////////////////
    parentFram.AddMode(false);
    parentFram.reload();
    parentFram.reloadResult();
   }
  });
  //-
  gc.gridwidth = 2;
  gc.gridheight = 1;
  gc.gridx=2;
  gc.gridy=12;
  JButton btnCancel = new JButton("Cancel");
  btnCancel.addActionListener(new ActionListener() {
   
   @Override
   public void actionPerformed(ActionEvent arg0) {
    parentFram.AddMode(false);
    parentFram.reload();
   }
  });
  btnPlus.setFocusPainted(false);
  this.add(btnCancel, gc);
  this.revalidate();
 }

 public void fillPanelPlace(){
  this.removeAll();
  this.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
  this.setLayout(new GridBagLayout());
  GridBagConstraints gc = new GridBagConstraints();
  gc.fill=GridBagConstraints.BOTH;
  //PARAMETER
  gc.weightx=1;
  gc.weighty=1;
  //---------------------------------------//
  //TITRE
  gc.gridheight=1;
  gc.gridwidth= 4;
  gc.gridx=0;
  gc.gridy=0;
  gc.insets = new Insets(0, 0, 5, 0);
  JPanel panelTitre = new JPanel(new BorderLayout());
  JLabel titre = new JLabel("Ajout " );
  
  panelTitre.setBackground(Color.white);
	titre.setBackground(Color.white);
	titre.setOpaque(true);
  
  titre.setVerticalAlignment(SwingConstants.CENTER);
  titre.setHorizontalAlignment(SwingConstants.CENTER);
  panelTitre.add(titre, BorderLayout.CENTER);
  this.add(panelTitre, gc);
  //--------------------------------RDC------------------------------//
  //ETAGE 01
  //label 
  gc.insets = new Insets(0, 10, 0, 0);
  gc.gridwidth = 1;
  gc.gridheight = 1;
  gc.gridx=0;
  gc.gridy=1;
  JLabel labelLabel = new JLabel("Label :");
  labelLabel.setHorizontalAlignment(SwingConstants.CENTER);
  this.add(labelLabel, gc);
  //field
  gc.insets = new Insets(5, 10, 5, 10);
  gc.gridwidth = 3;
  gc.gridheight = 1;
  gc.gridx=1;
  gc.gridy=1;
  JPanel panelFieldLabel = new JPanel(new BorderLayout(0,0));
  
  labelField.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
  labelField.setMargin(new Insets(50, 0, 0, 0));
  labelField.setBorder(BorderFactory.createCompoundBorder(
    labelField.getBorder(), 
        BorderFactory.createEmptyBorder(0, 10, 0, 10)));
  panelFieldLabel.add(labelField, BorderLayout.CENTER);
  this.add(panelFieldLabel, gc);
  
  //ETAGE TEXT LONG
  gc.insets = new Insets(3, 5, 3, 5);
  gc.gridwidth = 4;
  gc.gridheight = 2;
  gc.gridx=0;
  gc.gridy=2;
  //TEST
  JPanel panel = new JPanel();
  panel.setMinimumSize(new Dimension(1, 100));
  panel.setLayout(new BorderLayout()); 
  JScrollPane scroll = new JScrollPane(textArea);
  panel.add(scroll, BorderLayout.CENTER);
  //END TEST
  this.add(panel, gc);
  //ETAGE IMAGE 
  //BTN +
  gc.gridwidth = 2;
  gc.gridheight = 1;
  gc.gridx=0;
  gc.gridy=4;
  JButton btnPlus = new JButton("Ajout img");
  btnPlus.addActionListener(new ActionListener() {
   
  @Override
  public void actionPerformed(ActionEvent e) {    
   JFileChooser chooser = new JFileChooser();
      FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & GIF Images", "jpg", "gif","png");
      chooser.setFileFilter(filter);
      int returnVal = chooser.showOpenDialog(null);
      if(returnVal == JFileChooser.APPROVE_OPTION) {
       String y = chooser.getSelectedFile().getAbsolutePath();
       addImg(y);
      }     
  }
  });
  
  btnPlus.setFocusPainted(false);
  this.add(btnPlus, gc);
  //BTN -
  gc.gridwidth = 2;
  gc.gridheight = 1;
  gc.gridx=2;
  gc.gridy=4;
  JButton btnLess = new JButton("Supprime img");
  btnLess.addActionListener(new ActionListener() {
   @Override
   public void actionPerformed(ActionEvent arg0) {
    if (listeURL.getSelectedValue() != null) {
        int z= listeURL.getSelectedIndex();
     data = ArrayUtils.remove(data,z );
     fillPanelPlace();
    }
   }
  });
  
  this.add(btnLess, gc);
  //ETAGE LISTE  URL IMAGE
  gc.insets = new Insets(3, 5, 3, 5);
  gc.gridwidth = 4;
  gc.gridheight = 3;
  gc.gridx=0;
  gc.gridy=5;
  
  this.listeURL = new JList<>(data);
  this.listeURL.setVisibleRowCount(5);
  JScrollPane panelListImg = new JScrollPane(this.listeURL);
  this.add(panelListImg, gc);
 
  //ETAGE BTN
  gc.gridwidth = 2;
  gc.gridheight = 1;
  gc.gridx=0;
  gc.gridy=12;
  JButton btnSave = new JButton("Save");
  btnSave.setFocusPainted(false);
  this.add(btnSave, gc);
  btnSave.addActionListener(new ActionListener() {
   @Override
   public void actionPerformed(ActionEvent arg0) {
    //ICI LE AJOUT BDD//
    switch (parentFram.getMode()) {
    case ITINERARY:
     break;
    case PLACE:
    	Place newPlace = new Place();
    	newPlace.setLabel(labelField.getText());
    	newPlace.setText(textArea.getText());
        for (String img: data) {
        	newPlace.addImage(img); 
        }
        parentFram.getServicePlace().addPlace(newPlace);
     break;
    case NEWS:
    default: 
     break;
    }
    ////////////////////
    parentFram.AddMode(false);
    parentFram.reloadResult();
   }
  });
  //-
  gc.gridwidth = 2;
  gc.gridheight = 1;
  gc.gridx=2;
  gc.gridy=12;
  JButton btnCancel = new JButton("Cancel");
  btnCancel.addActionListener(new ActionListener() {
   
   @Override
   public void actionPerformed(ActionEvent arg0) {
    parentFram.AddMode(false);
    parentFram.reload();
   }
  });
  btnPlus.setFocusPainted(false);
  this.add(btnCancel, gc);
  this.revalidate();
 }
 
 public void addUrl(String x){
  String dataTemp[] = new String[data2.length+1]; 
  System.arraycopy(data2, 0, dataTemp, 0, data2.length);
  dataTemp[data2.length]=x;
  data2 = dataTemp;
  if (parentFram.getMode() == HomeFramMode.PLACE){
	  fillPanelPlace();
  } else {
	  fillPanel();
  }
 }
 public void addImg(String x){
  String dataTemp[] = new String[data.length+1]; 
  System.arraycopy(data, 0, dataTemp, 0, data.length);
  dataTemp[data.length]=x;
  data = dataTemp;
  if (parentFram.getMode() == HomeFramMode.PLACE){
	  fillPanelPlace();
  } else {
	  fillPanel();
  }
 }

}
